package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.lawyerinfo.LawyerInfoBo;
import org.summer.lawyer.domain.vo.lawyerinfo.LawyerInfoVo;
import org.summer.lawyer.service.ILawyerInfoService;

import java.util.List;

/**
 * 律师信息
 *
 * @author summer
 * @date 2024-06-25
 */
@Tag(name = "后台相关接口", description = "律师信息")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/lawyerInfo")
public class LawyerInfoController extends BaseController {

    private final ILawyerInfoService lawyerInfoService;

    /**
     * 查询律师信息列表
     */
    @Operation(summary = "查询律师信息列表接口", description = "查询律师信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    //@SaCheckPermission("lawfirm:lawyer:list")
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<LawyerInfoVo> list(LawyerInfoBo bo, PageQuery pageQuery) {
        if(bo.getLawFirmId() == null){
            bo.setLawFirmId(0L);
        }
        return lawyerInfoService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出律师信息列表
     */
    @Operation(summary = "导出律师信息列表接口", description = "导出律师信息列表")
    //@SaCheckPermission("lawfirm:lawyer:export")
    @SaCheckLogin
    @Log(title = "律师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LawyerInfoBo bo, HttpServletResponse response) {
        List<LawyerInfoVo> list = lawyerInfoService.selectLawyerInfoVoList(bo);
        ExcelUtil.exportExcel(list, "律师信息", LawyerInfoVo.class, response);
    }

    /**
     * 获取律师信息详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "获取律师信息详细信息接口", description = "查询律师信息列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    //@SaCheckPermission("lawfirm:lawyer:query")
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<LawyerInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(lawyerInfoService.selectLawyerInfoVoById(id));
    }

    /**
     * 新增律师信息
     */
    @Operation(summary = "新增律师信息接口", description = "新增律师信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawfirm:lawyer:add")
    @Log(title = "律师信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveLawyerInfo")
    public R<Void> saveLawyerInfo(@Validated(AddGroup.class) @RequestBody LawyerInfoBo bo) {
        return toAjax(lawyerInfoService.insertByBo(bo));
    }

    /**
     * 修改律师信息
     */
    @Operation(summary = "修改律师信息接口", description = "修改律师信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawfirm:lawyer:edit")
    @Log(title = "律师信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateLawyerInfo")
    public R<Void> updateLawyerInfo(@Validated(EditGroup.class) @RequestBody LawyerInfoBo bo) {
        return toAjax(lawyerInfoService.updateByBo(bo));
    }

    /**
     * 删除律师信息
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除律师信息接口", description = "删除律师信息", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("lawfirm:lawyer:remove")
    @Log(title = "律师信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeLawyerInfo/{ids}")
    public R<Void> removeLawyerInfo(@NotEmpty(message = "主键不能为空")
                                    @PathVariable Long[] ids) {
        return toAjax(lawyerInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
